//
//  ECMemberCommonSearchViewController.m
//  ECERP.CRM
//
//  Created by 刘思麟 on 2018/11/4.
//  Copyright © 2018 Mac. All rights reserved.
//

#import "ECMemberCommonSearchViewController.h"
#import "ECMemberCommonTableView.h"
#import "ECMemberCommonTableViewCell.h"
#import "ECMemberCommonHeaderFooterView.h"
#import "ECMemberCommonSelectViewController.h"

@interface ECMemberCommonSearchViewController ()<ECCommonTableViewDelegate,UITableViewDelegate,UITableViewDataSource,UISearchBarDelegate>

@property (nonatomic,strong) ECMemberCommonTableView * tableView;

@property (nonatomic,strong) UISearchBar * searchBar;

@property (nonatomic,strong) NSArray * dataArr;

@end

@implementation ECMemberCommonSearchViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (void)setPropsDic:(NSDictionary<NSString *,id> *)propsDic {
    _propsDic = propsDic;
    NSString * itemTitle = _propsDic[@"title"];
    self.navigationItem.title = itemTitle;
    if ([_propsDic objectForKey:@"addTitle"]) {
        self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"add_icon"] style:UIBarButtonItemStylePlain target:self action:@selector(doAdd)];
    }
}

- (void)setVModel:(ECMemberCommonSearchViewModel *)vModel {
    _vModel = vModel;
    vModel.returnBlock = ^(NSDictionary * dataDic) {
        self.dataArr = dataDic[@"data"];
        [self.tableView.tableView reloadData];
        BOOL haveMore = [dataDic[@"haveMore"] boolValue];
        if (!haveMore) {
            [self.tableView.tableView.mj_footer endRefreshingWithNoMoreData];
        }
    };
}

- (void)doAdd {
    if (self.propsDic) {
        ECMemberCommonSelectViewController * vc = [ECMemberCommonSelectViewController new];
        vc.navigationItem.title = self.propsDic[@"addTitle"];
        vc.dataArr = self.propsDic[@"props"];
        [self.navigationController pushViewController:vc animated:YES];
    }
}

- (void)loadSubView {
    UIView * view =  [[UIView alloc] initWithFrame:CGRectMake(0, 0, ECSCREEN_WIDTH, 54 * PX_RELATIVE375)];
    view.backgroundColor = [UIColor whiteColor];
    WS(weakV, view);
    [view addSubview:self.searchBar];
    self.searchBar.layer.cornerRadius = 16;
    self.searchBar.clipsToBounds = YES;
    self.searchBar.layer.borderWidth = 1;
    self.searchBar.layer.borderColor = ECCOLOR(0xcccccc).CGColor;
    [self.searchBar mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(weakV).mas_offset(ECINDENTATION_LEFT);
        make.centerY.mas_equalTo(weakV);
        make.right.mas_equalTo(weakV).mas_offset(-ECINDENTATION_LEFT);
        make.height.mas_equalTo(32 * PX_RELATIVE375);
    }];
    
    [self.view addSubview:view];
    
    [self.view addSubview:self.tableView];
    WS(weakSelf, self);
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        if (@available(iOS 11.0, *)) {
            make.left.mas_equalTo(weakSelf.view.mas_safeAreaLayoutGuideLeft);
            make.top.mas_equalTo(weakV.mas_safeAreaLayoutGuideBottom);
            make.right.mas_equalTo(weakSelf.view.mas_safeAreaLayoutGuideRight);
            make.bottom.mas_equalTo(weakSelf.view.mas_safeAreaLayoutGuideBottom);
        } else {
            make.top.mas_equalTo(weakV.mas_bottom);
            make.left.mas_equalTo(weakSelf.view);
            make.right.mas_equalTo(weakSelf.view);
            make.bottom.mas_equalTo(weakSelf.view);
        }
    }];
    [self.vModel fetchData];
}

- (UISearchBar *)searchBar {
    if (!_searchBar) {
        _searchBar = [UISearchBar new];
        [_searchBar setImage:[UIImage imageNamed:@"search_member"] forSearchBarIcon:UISearchBarIconSearch state:UIControlStateNormal];
        _searchBar.placeholder = @" ";
        [_searchBar setBackgroundImage:[UIImage imageWithColor:[UIColor whiteColor]]];
        [_searchBar setPositionAdjustment:UIOffsetMake(-6, 0) forSearchBarIcon:UISearchBarIconSearch];
        for (UIView * v in _searchBar.subviews.firstObject.subviews) {
            if ([v isKindOfClass:NSClassFromString(@"UISearchBarTextField")]) {
                UITextField * tf = (UITextField*)v;
                tf.font = ECFONT(14);
            }
        }
    }
    return _searchBar;
}

- (ECMemberCommonTableView *)tableView {
    if (!_tableView) {
        _tableView = [[ECMemberCommonTableView alloc] initWithFrame:CGRectZero];
        _tableView.tableView.backgroundColor = [UIColor whiteColor];
        _tableView.tableView.dataSource = self;
        _tableView.tableView.delegate = self;
        [_tableView.tableView registerClass:[ECMemberCommonTableViewCell class] forCellReuseIdentifier:@"cell"];
        [_tableView.tableView registerClass:[ECMemberCommonHeaderFooterView class] forHeaderFooterViewReuseIdentifier:@"headerV"];
        _tableView.ecDelegate = self;
    }
    return _tableView;
}

#pragma mark - ECCommonTableViewDelegate

- (NSArray<NSString *> *)titlesOfSectionIndex {
    return @[@"MMP",@"1",@"2"];
}

- (void)selectedTitleAtIndex:(NSInteger)index {
    if (index == 2) {
        [self.tableView.tableView scrollToBottomAnimated:NO];
    }
}

#pragma mark UITableViewDelegate,UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArr.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 27;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    ECMemberCommonHeaderFooterView * headerV = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"headerV"];
    return headerV;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    return @" 13";
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 44;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    ECMemberCommonTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    ECMemberCommonSearchModel * model = self.dataArr[indexPath.row];
    cell.textLabel.text = model.displayTitle;
    return cell;
}

#pragma mark -UISearchBarDelegate
- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar {
    [self.searchBar setPositionAdjustment:UIOffsetZero forSearchBarIcon:UISearchBarIconSearch];
}

- (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar {
    [self.searchBar setPositionAdjustment:UIOffsetMake(60 * PX_RELATIVE375, 0) forSearchBarIcon:UISearchBarIconSearch];
}

@end
